﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;
using PTA.TreeHelper;
namespace PTA.Auth
{
    [System.AttributeUsage(AttributeTargets.Field)]
    public class PrivilegeItemAttribute : Attribute
    {
        /// <summary>
        /// 父级分类
        /// </summary>
        [Description("功能点父级分类,使用|分隔,如:首页|显示待办")]
        public string Parent { get; set; }

        /// <summary>
        /// 显示名称
        /// </summary>
        [Description("功能点在鼠标划过时显示的说明")]
        public string Title { get; set; }

        public PrivilegeItemAttribute(string parent)
        {
            this.Parent = parent;
        }

        public PrivilegeItemAttribute(string parent, string title)
        {
            this.Parent = parent;
            this.Title = title;
        }
    }

    /// <summary>
    /// 角色接口
    /// </summary>
    public interface IRole
    {
        int RoleId { get; set; }
        List<int> PrivilegeList { get; set; }
    }

    /// <summary>
    /// 实体权限接口
    /// </summary>
    public interface IEntityAuth
    {
        /// <summary>
        /// 实体包含的角色列表
        /// </summary>
        [Description("实体包含的角色列表")]
        List<int> RoleList { get; }

        /// <summary>
        /// 实体包含的直接权限（菜单和功能点列表）
        /// </summary>
        [Description("实体包含的权限（菜单和功能点列表）")]
        List<int> IncludePrivilegeList { get; }

        /// <summary>
        /// 
        /// </summary>
        [Description("实体不包含的权限（菜单和功能点列表），如果检查的权限在这个列表里，则认为用户不具有权限")]
        List<int> ExcludePrivilegeList { get; }
    }

    public class MenuNode : TreeNode<MenuNode>
    {
        /// <summary>
        /// 页面地址
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 鉴权地址,如果不设置,则认为同Url
        /// </summary>
        public string AuthUrl { get; set; }

        /// <summary>
        /// 控件样式名称
        /// </summary>
        public string CssClass { get; set; }

        /// <summary>
        /// 菜单值 
        /// </summary>
        public int Value { get; set; }

        /// <summary>
        /// 是否可用
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool Visible { get; set; }

        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// 目标框架
        /// </summary>
        public string Target { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string ToolTip { get; set; }

        public MenuNode Clone()
        {
            var result = new MenuNode();
            result.Text = this.Text;
            result.Url = this.Url;
            result.Value = this.Value;
            this.Visible = this.Visible;
            result.ToolTip = this.ToolTip;
            result.AuthUrl = this.AuthUrl;
            result.CssClass = this.CssClass;
            result.Enabled = this.Enabled;
            result.ImageUrl = this.ImageUrl;
            result.Target = this.Target;
            foreach (var x in this.ChildNodes)
            {
                result.ChildNodes.Add(x.Clone());
            }
            return result;
        }
    }

    public class MenuTree : TreeView<MenuNode>
    {
        public MenuTree Clone()
        {
            var result = new MenuTree();
            foreach(var x in this.Nodes)
            {
                result.Add(x.Clone());
            }
            return result;
        }
    }
}
